﻿package bacIns 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import  flash.display.Sprite;
	import game.events.EventData;
	import game.GlobalVariables;
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)	 
	 */
	public class BacHistoryPanel extends MovieClip
	{
		private static const BANKER:String = "10";
		private static const BANKER_P:String = "11";
		private static const BANKER_PP:String = "12";
		private static const BANKER_BPP:String = "13";
		
		private static const PLAYER:String = "20";
		private static const PLAYER_BP:String = "21";
		private static const PLAYER_P:String = "22";
		private static const PLAYER_BPP:String = "23";
		
		private static const TIE:String = "30";
		private static const TIE_BP:String = "31";
		private static const TIE_PP:String = "32";
		private static const TIE_BPP:String = "33";
		private static const MAX_NUM_CHECKERS:int = 66;
		
		public var textBanker:String;
		public var textPlayer:String;
		public var textTie:String;
		
		private var rowNum:int = 6;
		private var rowInd:int = 0;
		private var colInd:int = 0;
		
		public var hisBoard:Sprite = new Sprite();
		public function BacHistoryPanel() 
		{
			addChild(hisBoard);
			textBanker = "B";
			textPlayer = "P";
			textTie = "T";
		}
		
		public function initBoard(resultArr:Array, resultNumArr:Array, vodId:String):void {
			removeHistory();
			if (resultArr.length == 0)
				return;
			var i:int;
			if (resultArr.length > BacHistoryPanel.MAX_NUM_CHECKERS) {
				i = Math.abs(resultArr.length - BacHistoryPanel.MAX_NUM_CHECKERS - rowNum - 1);
			}
			for (i ; i < resultArr.length; i++) {
				//drawIcon(resultArr[i]);
				var iconHis:MovieClip = attachIcon(resultArr[i], resultNumArr[i]);
				iconHis.name = vodId + (i+1).toString();
				if (rowInd + 1 == rowNum) {
					rowInd = 0;
					colInd++;
				} else rowInd++;
			}			
		}		
	
		private function attachIcon(type:String, pointNum:String):MovieClip {
				var hisIcon:MovieClip = MovieClip(new HistoryIcon());
				var icon:MovieClip = hisIcon.mc;
				hisIcon.addEventListener(MouseEvent.CLICK, onClickHandler);
				hisBoard.addChild(hisIcon);
				hisIcon.x = colInd * 23;
				hisIcon.y = rowInd * 23 - 0.3;
				hisIcon.scaleX = hisIcon.scaleY = 0.89;
				//hisIcon.width = 17;
				//hisIcon.height = 17;
				icon.bankerPair.visible = false;
				icon.playerPair.visible = false;
				/****/
				hisIcon.buttonMode = true;
				icon.tf.mouseEnabled = false;
				/****/
				// banker 
				if (type == BacHistoryPanel.BANKER) {
					icon.gotoAndStop("banker");
					if (GlobalVariables.bacShowPoint) {
						icon.tf.text = pointNum;
					}else {
						icon.tf.text = textBanker;
					}					
					return hisIcon;
				}
				if (type == BacHistoryPanel.BANKER_P) {
					icon.gotoAndStop("banker");
					icon.bankerPair.visible = true;
					if (GlobalVariables.bacShowPoint) {
						icon.tf.text = pointNum;
					}else {
						icon.tf.text = textBanker;
					}	
					return hisIcon;
				}
				if (type == BacHistoryPanel.BANKER_PP) {
					icon.gotoAndStop("banker");
					icon.playerPair.visible = true;
					if (GlobalVariables.bacShowPoint) {
						icon.tf.text = pointNum;
					}else {
						icon.tf.text = textBanker;
					}	
					return hisIcon;
				}
				if (type == BacHistoryPanel.BANKER_BPP) {
					icon.gotoAndStop("banker");
					icon.bankerPair.visible = true;
					icon.playerPair.visible = true;
					if (GlobalVariables.bacShowPoint) {
						icon.tf.text = pointNum;
					}else {
						icon.tf.text = textBanker;
					}	
					return hisIcon;
				}
				
				// player 
				if (type == BacHistoryPanel.PLAYER) {
					icon.gotoAndStop("player");
					if (GlobalVariables.bacShowPoint) {
						icon.tf.text = pointNum;
					}else {
						icon.tf.text = textPlayer;
					}						
					return hisIcon;
				}
				if (type == BacHistoryPanel.PLAYER_P) {
					icon.gotoAndStop("player");
					icon.playerPair.visible = true;
					if (GlobalVariables.bacShowPoint) {
						icon.tf.text = pointNum;
					}else {
						icon.tf.text = textPlayer;
					}	
					return hisIcon;
				}
				if (type == BacHistoryPanel.PLAYER_BP) {
					icon.gotoAndStop("player");
					icon.bankerPair.visible = true;
					if (GlobalVariables.bacShowPoint) {
						icon.tf.text = pointNum;
					}else {
						icon.tf.text = textPlayer;
					}	
					return hisIcon;
				}
				if (type == BacHistoryPanel.PLAYER_BPP) {
					icon.gotoAndStop("player");
					icon.bankerPair.visible = true;
					icon.playerPair.visible = true;
					if (GlobalVariables.bacShowPoint) {
						icon.tf.text = pointNum;
					}else {
						icon.tf.text = textPlayer;
					}	
					return hisIcon;
				}
				
				// tie
				if (type == BacHistoryPanel.TIE) {
					icon.gotoAndStop("tie");					
					if (GlobalVariables.bacShowPoint) {
						icon.tf.text = pointNum;
					}else {
						icon.tf.text = textTie;
					}	
					return hisIcon;
				}
				if (type == BacHistoryPanel.TIE_BP) {
					icon.gotoAndStop("tie");
					icon.bankerPair.visible = true;
					if (GlobalVariables.bacShowPoint) {
						icon.tf.text = pointNum;
					}else {
						icon.tf.text = textTie;
					}	
					return hisIcon;
				}
				if (type == BacHistoryPanel.TIE_PP) {
					icon.gotoAndStop("tie");
					icon.playerPair.visible = true;
					if (GlobalVariables.bacShowPoint) {
						icon.tf.text = pointNum;
					}else {
						icon.tf.text = textTie;
					}	
					return hisIcon;
				}
				if (type == BacHistoryPanel.TIE_BPP) {
					icon.gotoAndStop("tie");
					icon.bankerPair.visible = true;
					icon.playerPair.visible = true;
					if (GlobalVariables.bacShowPoint) {
						icon.tf.text = pointNum;
					}else {
						icon.tf.text = textTie;
					}	
					return hisIcon;
				}		
				return null;
		}
		private function onClickHandler(e:MouseEvent) {
			if(e.currentTarget.name!="ask")
			{
				//trace("I waited for Press AND Release!!!" + e.currentTarget.name);
				var event:EventData = new EventData(EventData.PLAY_VOD, true);
				event.ms = e.currentTarget.name;
				dispatchEvent(event);
			}
		}
		
		/*private function removeHistory():void {
			
			var numberChid:int = hisBoard.numChildren;
			//trace("......" + hisBoard.numChildren);
			if (numberChid> 0)
			{
				for (var j:int = 0; j < numberChid; j++ ) 
				{
					var obj:DisplayObject = hisBoard.getChildByName(j.toString());
					hisBoard.removeChild(obj);
					trace("child:" + j + ":" + numberChid );
				}
				rowInd = 0;
				colInd = 0;
			}
		}*/
		private function removeHistory():void {
			
			var numberChid:int = hisBoard.numChildren;
			if (numberChid> 0)
			{
				for (var j:int = 0; j < numberChid; j++ ) 
				{
					hisBoard.removeChildAt(0);
					
				}
				rowInd = 0;
				colInd = 0;
			}
		}
		
		private function removeIterm(boad:Sprite, nameIterm:String):void {
			var obj:DisplayObject = boad.getChildByName(nameIterm);
			if (obj != null)
				boad.removeChild(obj);
		}
		public function askBankerPlayer(num:String, sLoop:String) {
			if (sLoop != "N")
			{
				removeIterm(hisBoard, "ask");			
				var iconHis:MovieClip = attachIcon(num, "");
				iconHis.name = "ask";
				iconHis.gotoAndPlay("loop");
			}
			
		}
	}

}